import 'package:extended_tabs/extended_tabs.dart';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:ball_lion/resources/colours.dart';
import 'package:ball_lion/utils/date_time_utils.dart';
import 'package:ball_lion/widgets/base/wqs_indicator.dart';

import 'base/wqs_text.dart';

class DateTabBar extends StatefulWidget {
  const DateTabBar({
    super.key,
    required this.dateList,
    this.controller,
    this.onTap,
    this.indicatorSize,
    this.labelColor = Colours.text,
    this.labelStyle = const TextStyle(height: 2),
    this.labelPadding,
    this.indicatorPadding = EdgeInsets.zero,
    this.unselectedLabelColor = Colours.grey99,
    this.textStyle = const TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
    this.subTextStyle =
        const TextStyle(fontSize: 12, fontWeight: FontWeight.w500),
    this.indicator = const QsIndicator(
        borderSide: BorderSide(color: Colours.main, width: 4),
        indicatorWidth: 1,
        indicatorBottom: 4),
    this.isScrollable = true,
    this.labelInnerPadding,
    this.dataFormat = 'MM/dd',
    this.textSpacing = 0,
    this.labelAbove,
    this.showAbove,
  });

  final Decoration indicator;
  final TextStyle? labelStyle;
  final EdgeInsetsGeometry? labelPadding;
  final Color? labelColor;
  final TabController? controller;
  final List<DateTime> dateList;
  final ValueChanged<int>? onTap;
  final EdgeInsets? labelInnerPadding;
  final TabBarIndicatorSize? indicatorSize;
  final Color? unselectedLabelColor;
  final bool isScrollable;
  final String dataFormat;
  final double textSpacing;
  final EdgeInsetsGeometry indicatorPadding;
  final TextStyle? textStyle;
  final TextStyle? subTextStyle;
  final Widget? labelAbove;
  final List<int>? showAbove;

  @override
  State<DateTabBar> createState() => DateTabBarState();
}

class DateTabBarState extends State<DateTabBar> {
  @override
  Widget build(BuildContext context) {
    return ExtendedTabBar(
      physics: const ClampingScrollPhysics(),
      onTap: (value) {
        widget.onTap?.call(value);
      },
      tabs: List.generate(
          widget.dateList.length,
          (index) => Stack(
                children: [
                  Container(
                    padding: widget.labelInnerPadding,
                    child: Column(
                      mainAxisAlignment: MainAxisAlignment.center,
                      children: [
                        Expanded(
                          child: QsText.style(
                            widget.dateList[index].day == DateTime.now().day
                                ? "今天"
                                : DateFormat.E('zh_cn')
                                    .format(widget.dateList[index]),
                            style: widget.textStyle,
                          ),
                        ),
                        Container(height: widget.textSpacing),
                        QsText.style(
                          DateTimeUtils.formatDateTime(
                              widget.dateList[index], widget.dataFormat),
                          style: widget.subTextStyle,
                          needCenter: true,
                        ),
                      ],
                    ),
                  ),
                  if (widget.showAbove?.contains(index) == true)
                    widget.labelAbove ?? Container()
                ],
              )),
      indicator: widget.indicator,
      indicatorPadding: widget.indicatorPadding,
      labelPadding: widget.labelPadding,
      controller: widget.controller,
      indicatorSize: widget.indicatorSize,
      isScrollable: widget.isScrollable,
      labelColor: widget.labelColor,
      unselectedLabelColor: widget.unselectedLabelColor,
      labelStyle: widget.labelStyle,
    );
  }
}
